<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <div class="contentsize">合同查询</div>
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form ref="form" :model="form" label-width="auto" :inline="true" size="mini">
                    <el-form-item label="合同编号:">
                        <div style="width: 160px">
                            <el-input
                                v-model="form.htbh"
                                placeholder="请输入合同编号"
                                clearable
                                @change="onSearch"
                                style="width: 100%"
                            ></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="合同名称:">
                        <div style="width: 160px">
                            <el-input
                                v-model="form.htmc"
                                placeholder="请输入合同名称"
                                clearable
                                @change="onSearch"
                                style="width: 100%"
                            ></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="合同类别:">
                        <div style="width: 160px">
                            <el-select v-model="form.htlb" placeholder="请选择合同类别" @change="onSearch" clearable style="width: 100%">
                                <el-option v-for="item in sfklblist" :key="item.code" :label="item.name" :value="item.code"> </el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                    <!-- <el-form-item label="合同状态:">
                        <div style="width: 160px">
                            <el-select v-model="form.htzt" placeholder="请选择合同状态" @change="onSearch" clearable style="width: 100%">
                                <el-option v-for="item in htztlist" :key="item.code" :label="item.name" :value="item.code"> </el-option>
                            </el-select>
                        </div>
                    </el-form-item> -->
                    <el-form-item label="乙方单位:">
                        <el-input v-model="form.yf" placeholder="请输入乙方单位" clearable @change="onSearch"></el-input>
                    </el-form-item>
                    <el-form-item label="合同签订日期:">
                        <el-col :span="11">
                            <div style="width: 160px">
                                <el-date-picker
                                    type="date"
                                    placeholder="选择日期"
                                    v-model="form.startTime"
                                    value-format="yyyy-MM-dd"
                                    style="width: 100%"
                                    clearable
                                ></el-date-picker>
                            </div>
                        </el-col>
                        <el-col class="line" :span="2" style="text-align: center">至</el-col>
                        <el-col :span="11">
                            <div style="width: 160px">
                                <el-date-picker
                                    placeholder="选择日期"
                                    v-model="form.endTime"
                                    style="width: 100%"
                                    value-format="yyyy-MM-dd"
                                    type="date"
                                    clearable
                                ></el-date-picker>
                            </div>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="部门:" v-if="type=='0'">
                        <div style="width:170px">
                            <el-select v-model="form.type" placeholder="请选择部门" @change="onSearch">
                                <el-option v-for="item in yhlxlist" :key="item.code" :label="item.name"
                                    :value="item.code">
                                </el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSearch" icon="el-icon-search" class="searchcolor">查询</el-button>
                        <el-button @click="reSearch" icon="el-icon-refresh" class="searchcolor">重置</el-button>
                        <el-button type="primary" @click="downloadlist" icon="el-icon-download" class="searchcolor">导出</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table
                :data="tableData"
                style="width: 100%"
                :header-cell-style="tableHeaderColor"
                :row-style="tablerowColor"
                :row-class-name="tableRowClassName"
                :cell-style="tablecellColor"
                border
                :max-height="maxHeight"
                show-summary
                sum-text="合计"
                :summary-method="getSummaries"
            >
                <el-table-column
                    label="合同编号"
                    align="center"
                    prop="htbh"
                    fixed
                    width="160"
                    :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column label="合同名称" align="center" prop="htmc" width="180" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="合同类别" align="center" prop="htlb" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="甲方单位" align="center" prop="jf" width="240" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="乙方单位" align="center" prop="yf" width="240" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="合同金额（元）" align="center" prop="htje" width="120"></el-table-column>
                <el-table-column label="收（支）金额" align="center" prop="yszje" width="120"></el-table-column>
                <el-table-column label="签订日期" align="center" prop="qdrq" width="120">
                    <template slot-scope="scope">
                        {{ scope.row.qdrq ? scope.row.qdrq.split(' ')[0] : '' }}
                    </template>
                </el-table-column>
                <el-table-column label="起始日期" align="center" prop="qsrq" width="120">
                    <template slot-scope="scope">
                        {{ scope.row.qsrq ? scope.row.qsrq.split(' ')[0] : '' }}
                    </template>
                </el-table-column>
                <el-table-column label="截止日期" align="center" prop="jzrq" width="120">
                    <template slot-scope="scope">
                        {{ scope.row.jzrq ? scope.row.jzrq.split(' ')[0] : '' }}
                    </template>
                </el-table-column>
                <el-table-column label="录入日期" align="center" prop="lrrq" width="120">
                    <template slot-scope="scope">
                        {{ scope.row.lrrq ? scope.row.lrrq.split(' ')[0] : '' }}
                    </template>
                </el-table-column>
                <el-table-column label="经手人" align="center" prop="jsr"></el-table-column>
                <el-table-column label="审批时间" align="center" prop="spsj" width="120">
                    <template slot-scope="scope">
                        {{ scope.row.spsj ? scope.row.spsj.split(' ')[0] : '' }}
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination" style="float: right">
                <el-pagination
                    background
                    layout="total,prev, pager, next,jumper"
                    :total="total"
                    :current-page.sync="current"
                    :page-size="pageSize"
                    @current-change="handleCurrentChange"
                >
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { allsignlist, allsignlisthj, exportallsignlist } from '@/api/index';
import valueList from '@/components/common/valueList.vue';
export default {
    name: 'yyhtxqlist',
    data() {
        return {
            sfklblist: [
                { name: '收入', code: '收入' },
                { name: '支出', code: '支出' },
                { name: '无', code: '无' }
            ], //合同类别
            htztlist: valueList.htztlist, //合同状态
            total: 0,
            current: 1,
            pageSize: 10,
            form: {
                create_time: this.$store.getters.type!='1'?"2025-02-01":'',
                htbh: '',
                htmc: '',
                htlb: '',
                yf: '',
                startTime: null,
                endTime: null,
                pageSize: 10,
                current: 1,
                type: this.$store.getters.type == '0' ? '2' : this.$store.getters.type
            },
            tableData: [],
            maxHeight: null,
            hjje: {},
            yhlxlist: valueList.yhlxlist1,
            type: this.$store.getters.type,
        };
    },
    components: {},
    computed: {},
    methods: {
        // 列表样式
        tableHeaderColor({ row, column, rowIndex, columnIndex }) {
            if (rowIndex === 0) {
                return 'background:linear-gradient(to bottom, #008A94,#0ae2f2)!important;color:#ffffff;font-size:14px;';
            }
        },
        tablerowColor({ row, rowIndex }) {
            return 'height: 42px;font-size:14px;color:black';
        },
        tablecellColor() {
            return 'font-weight:700;color:#000000;border-color:#c0c0c0';
        },
        tableRowClassName({ row, rowIndex }) {
            if (rowIndex % 2 != 0) {
                return 'success-row'; //这是类名
            } else {
                return '';
            }
        },
        // 换页
        handleCurrentChange(val) {
            this.current = val;
            this.form.current = val;
            this.allsignlist(this.form);
            console.log(val);
        },
        // 搜索
        onSearch() {
            this.current = 1;
            this.form.current = 1;
            this.form.create_time=this.form.type=='2'?"2025-02-01":''
            this.allsignlist(this.form);
            this.allsignlisthj(this.form);
            console.log('搜索');
        },
        // 重置
        reSearch() {
            this.current = 1;
            this.form = {
                create_time: this.$store.getters.type!='1'?"2025-02-01":'',
                htbh: '',
                htlb: '',
                yf: '',
                htmc: '',
                startTime: null,
                endTime: null,
                pageSize: 10,
                current: 1,
                type: this.$store.getters.type == '0' ? '2' : this.$store.getters.type
            };
            this.allsignlist(this.form);
            this.allsignlisthj(this.form);
            console.log('搜索');
        },
        // 项目列表
        allsignlist(data) {
            allsignlist(data).then((res) => {
                if (res.code == 200) {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                } else {
                    this.tableData = [];
                    this.total = 0;
                }
            });
        },
        // 金额合计
        allsignlisthj(data) {
            allsignlisthj(data).then((res) => {
                console.log('备注', res.data);
                this.hjje = res.data;
            });
        },
        // 合并计算某一列
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '合计';
                    return;
                }
                // 只对amount这一列进行总计核算。
                if (column.property === 'htje') {
                    console.log('111', this.hjje);
                    sums[index] = this.hjje.htje;
                }else if (column.property === 'yszje'){
                    sums[index] = this.hjje.yszje;
                } else {
                    sums[index] = '---';
                }
            });
            return sums;
        },
        // 导出列表
        downloadlist() {
            exportallsignlist(this.form).then((res) => {
                console.log(res);
                const link = document.createElement('a');
                let blob = new Blob([res], {
                    type: 'application/vnd.ms-excel'
                });
                link.style.display = 'none';
                //设置连接
                link.href = URL.createObjectURL(blob);
                link.download = '合同查询导出文件';
                document.body.appendChild(link);
                //模拟点击事件
                link.click();
                console.log(res);
            });
        }
    },
    mounted() {
        this.$nextTick(() => {
            // window.innerHeight 浏览器窗口的可见高度，下面的 420 是除了table最大高度的剩余空间。
            this.maxHeight = window.innerHeight - 360;
        });
        console.log(this.$store.getters);
        this.allsignlist(this.form);
        this.allsignlisthj(this.form);
        console.log(valueList);
    }
};
</script>

<style scoped>
.container {
    /* height: calc(100vh - 260px); */
    overflow: hidden;
}

.crumbs {
    background-color: #fff;
    padding: 20px 20px;
}

.indexTitle {
    margin-bottom: 20px;
}

.contentsize {
    font-size: 18px;
    font-weight: 700;
}

.handle-box {
    overflow: hidden;
}

/* 新增按钮 */
.addcolor {
    margin-bottom: 10px;
}

.unloadTitle {
    text-align: left;
    padding-bottom: 10px;
    box-sizing: border-box;
    color: #606266;
}
</style>
<style>
.el-table__footer-wrapper tbody td.el-table__cell {
    font-weight: 600;
    background: rgb(246, 238, 217);
}

.tablestyle > .el-table {
    overflow: auto;
}

.tablestyle > .el-table .el-table__body-wrapper,
.tablestyle > .el-table .el-table__header-wrapper,
.tablestyle > .el-table .el-table__footer-wrapper {
    overflow: visible;
}

.tablestyle > .el-table::after {
    position: relative !important;
}
.success-row {
    background-color: #eaf3fb !important;
}
</style>
